package com.brillilab.starter.controller.web.instrument;

import com.brillilab.common.entity.PageVo;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.po.instrument.Instrument;
import com.brillilab.domain.vo.instrument.InstrumSearchVo;
import com.brillilab.domain.vo.instrument.InstrumentTo;
import com.brillilab.domain.vo.instrument.InstrumentVo;
import com.brillilab.domain.vo.instrument.MaintenanceVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.instrument.IInstrumentService;
import com.brillilab.service.logic.instrument.InstrumentLogic;
import com.brillilab.service.logic.instrument.converter.InstrumentConverter;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * <p>
 * 仪器 前端控制器
 * </p>
 *
 * @author WuMenghao
 * @since 2019-06-12
 */
@RestController
@RequestMapping("/web/instrument")
public class InstrumentController {

    @Resource
    private InstrumentLogic instrumentLogic;
    @Resource
    private IInstrumentService instrumentService;
    @Resource
    private UsersLogic usersLogic;

    /**
     * 查询
     *
     * @return
     */
    @RequestMapping(value="/search", method=RequestMethod.GET)
    public ResponseVo searchInstrument(InstrumSearchVo searchVo,@RequestHeader(value=StarterConstant.TOKEN) String token) {

        Assert.isTrue(searchVo != null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        //仪器搜索业务
        PageVo<InstrumentVo> instrumentToPageVo=instrumentLogic.searchForInstrument(searchVo,userInfo);

        Map<String, Object> map=new LinkedHashMap<String, Object>();
        map.put("total",instrumentToPageVo.getTotalRow());
        map.put("totalPage",instrumentToPageVo.getTotalPage());
        map.put("currentPage",instrumentToPageVo.getPageNum());
        map.put("instruments",instrumentToPageVo.getContent());
        return ResponseVo.success(map);
    }

    /**
     * 仪器添加
     *
     * @param instrumentVo
     * @return
     */
    @AspectLog
    @RequestMapping(value="/addInstrument", method=RequestMethod.POST)
    public ResponseVo addInstrument(@RequestBody InstrumentVo instrumentVo,@RequestHeader(value=StarterConstant.TOKEN) String token) {

        //参数校验
        Assert.isTrue(instrumentVo != null && StringUtils.isNotBlank(token) && StringUtils.isNotBlank(instrumentVo.getName())
                && instrumentVo.getDutyId() != null && StringUtils.isNotBlank(instrumentVo.getImage())
                && instrumentVo.getState() != null && instrumentVo.getNo() != null,"请求参数缺失！");

        if(instrumentVo.getMaintenances() != null && instrumentVo.getMaintenances().size() != 0){
            for (MaintenanceVo maintenanceVo : instrumentVo.getMaintenances()) {
                Assert.isTrue(StringUtils.isNotBlank(maintenanceVo.getStartTime()) && StringUtils.isNotBlank(maintenanceVo.getEndTime())
                        && maintenanceVo.getStartDay() != null && maintenanceVo.getEndDay() != null && maintenanceVo.getType() != null,"请求参数缺失！");
            }
        }

        //用户信息
        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        //添加业务
        Instrument instrument=instrumentLogic.createNewInstrument(instrumentVo,userInfo);

        return ResponseVo.success(instrument);

    }

    /**
     * 仪器信息回显
     *
     * @param id
     * @return
     */
    @RequestMapping(value="/recallInstrument", method=RequestMethod.GET)
    public ResponseVo recallInstrument(Long id,@RequestHeader(value=StarterConstant.TOKEN) String token) {

        //参数校验
        Assert.notNull(id,"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        //仪器信息信息回显业务
        InstrumentTo instrumentTo=instrumentLogic.recallInstrument(id,userInfo.getLabId());

        //转换得到VO对象
        InstrumentVo instrumentVo=InstrumentConverter.instrumentToInstrumentVoBack(instrumentTo);

        return ResponseVo.success(instrumentVo);
    }

    /**
     * 仪器列表
     *
     * @return
     */
    @RequestMapping(value="/list", method=RequestMethod.GET)
    public ResponseVo instrumentList(@RequestParam(name="pageNum", defaultValue="1") Integer pageNum,
                                     @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                     @RequestParam(name="isManager", defaultValue="false") Boolean isManager,
                                     @RequestHeader(value=StarterConstant.TOKEN) String token) {

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        //仪器列表
        PageVo<InstrumentVo> instrumentToPage=instrumentLogic.getInstrumentList(pageNum,pageSize,isManager,userInfo);

        Map<String, Object> map=new LinkedHashMap<String, Object>();
        map.put("total",instrumentToPage.getTotalRow());
        map.put("totalPage",instrumentToPage.getTotalPage());
        map.put("currentPage",instrumentToPage.getPageNum());
        map.put("instruments",instrumentToPage.getContent());
        return ResponseVo.success(map);
    }

    /**
     * 修改仪器
     * @return
     */
    @AspectLog
    @RequestMapping(value = "/modifyInstrument",method = RequestMethod.PUT)
    public ResponseVo modifyInstrument(@RequestBody InstrumentVo instrumentVo,@RequestHeader(value=StarterConstant.TOKEN) String token){

        //参数校验
        Assert.isTrue(instrumentVo != null && StringUtils.isNotBlank(token) && StringUtils.isNotBlank(instrumentVo.getName())
                && instrumentVo.getDutyId() != null && StringUtils.isNotBlank(instrumentVo.getImage())
                && instrumentVo.getState() != null && instrumentVo.getNo() != null,"请求参数缺失！");

        if(instrumentVo.getMaintenances() != null && instrumentVo.getMaintenances().size() != 0){
            for (MaintenanceVo maintenanceVo : instrumentVo.getMaintenances()) {
                Assert.isTrue(StringUtils.isNotBlank(maintenanceVo.getStartTime()) && StringUtils.isNotBlank(maintenanceVo.getEndTime())
                        && maintenanceVo.getStartDay() != null && maintenanceVo.getEndDay() != null && maintenanceVo.getType() != null,"请求参数缺失！");
            }
        }

        //获取用户信息缓存
        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        //状态矫正（数据库中没有2 维护 只有 0 正常 1 停用）
        if(instrumentVo.getState()==2){
            instrumentVo.setState(1);
        }

        //转换参数
        InstrumentTo instrumentTo = InstrumentConverter.instrumentVoToInstrumentTo(instrumentVo);

        //更新操作业务
        Instrument instrument = instrumentLogic.modifyInstrument(instrumentTo,userInfo);

        return ResponseVo.success(instrument);

    }

    /**
     * 删除仪器
     * @param instrumId
     * @return
     */
    @AspectLog
    @RequestMapping(value = "/deleteInstrument",method = RequestMethod.DELETE)
    public ResponseVo deleteInstrument(Long instrumId,@RequestHeader(value=StarterConstant.TOKEN) String token){

        //校验参数
        Assert.isTrue(instrumId!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        //删除业务
        instrumentLogic.getInstrumentDelete(instrumId,userInfo);

        return ResponseVo.success();
    }

    /**
     * 获取已存在的仪器编号
     * @return
     */
    @RequestMapping(value="/usedNo",method=RequestMethod.GET)
    public ResponseVo  usedInstrumentNo(@RequestHeader(value=StarterConstant.TOKEN) String token){

        Assert.isTrue(StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        Integer[] nos = instrumentService.getUsedInstrumentNo(userInfo.getLabId());

        return ResponseVo.success(nos);
    }
}
